package org.arrendar.kakeibo.view;

import java.security.Principal;

import javax.servlet.http.HttpServletRequest;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.link.ExternalLink;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class TopPage extends WebPage{

	public TopPage(){
		HttpServletRequest req = getWebRequestCycle().getWebRequest().getHttpServletRequest();
		UserService userService = UserServiceFactory.getUserService();
		String thisURL = req.getRequestURI();
		Principal principal=req.getUserPrincipal();
		if (principal != null) {
			String LogoutUrl = userService.createLogoutURL(thisURL);
			String UserId = userService.getCurrentUser().getUserId();
			PageParameters pram = new PageParameters();
			pram.put("Logout",LogoutUrl);
			pram.put("User",UserId);
			setResponsePage(MainPage.class,pram);
		} else {
		    String LoginUrl = userService.createLoginURL(thisURL);
		    ExternalLink link = new ExternalLink("LinkToLogin",LoginUrl);
		    add(link);
		}
	}
}
